package com.ghn.cocknovel.ui.activity.address

import chihane.jdaddressselector.AddressProvider
import chihane.jdaddressselector.model.City
import chihane.jdaddressselector.model.County
import chihane.jdaddressselector.model.Province
import chihane.jdaddressselector.model.Street
import com.ghn.cocknovel.net.DataService
import com.kt.NetworkModel.bean.MyAddressBean
import com.kt.NetworkModel.net.CallBack
import com.kt.NetworkModel.net.RxUtils

/**
 *@author: Administrator
 *2023/6/28
 *desc:
 */
class MyAddressProvider : AddressProvider {

    override fun provideProvinces(addressReceiver: AddressProvider.AddressReceiver<Province>) {
        DataService.queryAddress().compose(RxUtils.mainSync())
            .subscribe(object : CallBack<MutableList<MyAddressBean>>() {
                override fun onResult(result: MutableList<MyAddressBean>?) {
                    result?.also {
                        val provinces: ArrayList<Province> = ArrayList<Province>()
                        for ((index, value) in it.withIndex()) {
                            val province = Province()
                            province.id = value.districtId
                            province.name = value.districtName
                            provinces.add(province)
                        }
                        addressReceiver.send(provinces)
                    }
                }
            })
    }

    override fun provideCitiesWith(provinceId: Int, addressReceiver: AddressProvider.AddressReceiver<City>) {
        DataService.queryAddress(provinceId).compose(RxUtils.mainSync())
            .subscribe(object : CallBack<MutableList<MyAddressBean>>() {
                override fun onResult(result: MutableList<MyAddressBean>?) {
                    result?.also {
                        val provinces: ArrayList<City> = ArrayList<City>()
                        for ((index, value) in it.withIndex()) {
                            val province = City()
                            province.id = value.districtId
                            province.name = value.districtName
                            provinces.add(province)
                        }
                        addressReceiver.send(provinces)
                    }
                }
            })
    }

    override fun provideCountiesWith(cityId: Int, addressReceiver: AddressProvider.AddressReceiver<County>) {
        DataService.queryAddress(cityId).compose(RxUtils.mainSync())
            .subscribe(object : CallBack<MutableList<MyAddressBean>>() {
                override fun onResult(result: MutableList<MyAddressBean>?) {
                    result?.also {
                        val provinces: ArrayList<County> = ArrayList<County>()
                        for ((index, value) in it.withIndex()) {
                            val province = County()
                            province.id = value.districtId
                            province.name = value.districtName
                            provinces.add(province)
                        }
                        addressReceiver.send(provinces)
                    }
                }
            })
    }

    override fun provideStreetsWith(
        countyId: Int,
        addressReceiver: AddressProvider.AddressReceiver<Street>
    ) {
        addressReceiver.send(ArrayList<Street>())
    }
}